//
//  network.m
//  ChatRoom
//
//  Created by lee han on 12/10/12.
//  Copyright (c) 2012 lee han. All rights reserved.
//

#import "network.h"
#import "MBProgressHUD.h"
#import "CommonView.h"
#import "CRUserData.h"

network* g_netwok = nil;

@implementation network

+(network*)sharedInstance
{
    @synchronized(self)
    {
        if (g_netwok == nil)
        {
            g_netwok = [[network alloc] init];
        }
    }
    
    return g_netwok;
}



- (void)startHttpRequest:(id<networkDelegate>) del Data:(NSMutableDictionary *)data functionType:(NSInteger)type
{
    NSString *nsCmd = getFunctionName(type);
	if (nsCmd == nil)
		return;
    
    
    if(type != NetworkFunctionType_Login
       && type != NetworkFunctionType_ModifyPassword
       && NetworkFunctionType_Signup){
        if([[CRUserData sharedInstance] m_token] == nil)
            return;

        [data setValue:[[CRUserData sharedInstance] m_token] forKey:@"token"];
    }
    
    NSString* dataString = [CommonView toJSONData:data];
    
	NSString *nsUrl = [NSString stringWithFormat:@"%@%@", HOST_IP, nsCmd];
	NSData *body = [dataString dataUsingEncoding:NSUTF8StringEncoding];
	NSURL *url = [NSURL URLWithString:nsUrl];
	
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
	request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                        del, @"delegate",
                        [NSString stringWithFormat:@"%ld", (long)type], @"functionType", nil];
	[request appendPostData:body];
	[request setRequestMethod:@"POST"];
	[request setDelegate:self];
	[request startAsynchronous];
    
    [CommonView addLoadingView];
}


- (void)startSynchronousHttpRequest:(id<networkDelegate>) del Data:(NSDictionary *)data functionType:(NSInteger)type
{
    NSString *nsCmd = getFunctionName(type);
	if (nsCmd == nil)
		return;
    
    NSString* dataString = [CommonView toJSONData:data];
    
	NSString *nsUrl = [NSString stringWithFormat:@"%@%@", HOST_IP, nsCmd];
	NSData *body = [dataString dataUsingEncoding:NSUTF8StringEncoding];
	NSURL *url = [NSURL URLWithString:nsUrl];
	
	ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
	request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                        del, @"delegate",
                        [NSString stringWithFormat:@"%ld", (long)type], @"functionType", nil];
	[request appendPostData:body];
	[request setRequestMethod:@"POST"];
	[request setDelegate:self];
	[request startSynchronous];
}

#pragma mark ASIHTTPRequestDelegate

- (void)requestFinished:(ASIHTTPRequest *)request
{
    [CommonView removeLoadingView];
    
    id<networkDelegate> requestDelegate = [request.userInfo objectForKey:@"delegate"];
	NSInteger functionType = [[request.userInfo objectForKey:@"functionType"] integerValue];
    NSString *responseString = [request responseString];
    
    NSLog(@"%@", responseString);
    NSDictionary* responseDic = [NSDictionary dictionaryWithDictionary:[CommonView toNSDictionary:[responseString dataUsingEncoding:NSUTF8StringEncoding]]];
    if([responseDic count] == 0){
        NSLog(@"网络错误");
        return;
    }
    if (requestDelegate)
    {
        [requestDelegate parserResult:responseDic functionType:functionType];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSString* errmsg = [[request error] localizedDescription];
    
    NSLog(@"requestFailed %@", errmsg);
}
//end ASIHTTPRequestDelegate
@end
